Imports System.Collections
Imports System.Collections.Generic
Partial Class Admin_EditUser
    Inherits System.Web.UI.Page

    Dim userName As String = ""
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        userName = Me.Request.QueryString("UserName")
        If Me.Request.QueryString("Username") Is Nothing Then
            Response.Redirect("~/admin/ManageUsers.aspx")
        End If
        lblRolesFeedbackOK.Visible = False

        If Not Me.IsPostBack Then
            Dim user As MembershipUser = Membership.GetUser(userName)
            lblUserName.Text = user.UserName
            lnkEmail.Text = user.Email
            lnkEmail.NavigateUrl = "mailto:" & user.Email
            BindRoles()
        End If
    End Sub

    Private Sub BindRoles()
        chklRoles.DataSource = Roles.GetAllRoles
        chklRoles.DataBind()
        For Each role As String In Roles.GetRolesForUser(userName)
            chklRoles.Items.FindByText(role).Selected = True
        Next
    End Sub

    Protected Sub btnUpdateRoles_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdateRoles.Click
        Dim currRoles() As String = Roles.GetRolesForUser(userName)
        If currRoles.Length > 0 Then
            Roles.RemoveUserFromRoles(userName, currRoles)
        End If

        Dim newRoles As New List(Of String)
        For Each item As ListItem In chklRoles.Items
            If item.Selected Then
                newRoles.Add(item.Text)
            End If
        Next
        Dim userNames() As String = {userName}
        Roles.AddUsersToRoles(userNames, newRoles.ToArray())

        lblRolesFeedbackOK.Visible = True
    End Sub

    Protected Sub btnCreateRole_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateRole.Click
        If Not Roles.RoleExists(txtNewRole.Text.Trim) Then
            Roles.CreateRole(txtNewRole.Text.Trim)
            BindRoles()
        End If
    End Sub

End Class
